/**
 *  @file SDLFader.cpp
 *
 *  @date 28-nov-2009
 *  @author Ives van der Flaas
 */

#include <iostream>
#include "SDLFader.h"
#include <SDL_gfxPrimitives.h>

namespace si_sdl
{
void SDLFader::setRect(si::Vector2 pos, si::Vector2 size)
{
	fPos = pos;
	fSize = size;
}

SDLFader::~SDLFader()
{
}

}

void si_sdl::SDLFader::draw()
{
	if((fSize.getX() < 1) && (fSize.getY() < 1))
	{
		fPos.setX(SDL_GetVideoSurface()->w);
		fPos.setY(SDL_GetVideoSurface()->h);
	}


	const int opacity = 255*fCurrentOpacity; // convert from percent to 0 - 255
	boxRGBA(SDL_GetVideoSurface(),
			fPos.getX(), fPos.getY(),
	        fSize.getX(), fSize.getY(),
	        fColor.getR(), fColor.getG(), fColor.getB(),
	        opacity);
}



void si_sdl::SDLFader::update(double time)
{
	const double change = (fSpeed * time)/100;

	if(fCurrentOpacity >= -0.01 && fCurrentOpacity <= 1.01)
	{
		if(fType == FADE_TO_COLOR)
			fCurrentOpacity += change;

		if(fType == FADE_FROM_COLOR)
			fCurrentOpacity -= change;
	}

	if(fCurrentOpacity > 1.0)
		fCurrentOpacity = 1.0;
	if(fCurrentOpacity < 0.0)
		fCurrentOpacity = 0.0;
}



void si_sdl::SDLFader::reset(Color c, FadeType type, double speed)
{
	fSpeed = speed;
	fType = type;
	fColor = c;

	fCurrentOpacity = (fType == FADE_TO_COLOR ? 0.0 : 1.0);
}


